package cn.yuanqiao.archive.guangpanguanli.service.impl;

import cn.yuanqiao.archive.appManager.domain.*;
import cn.yuanqiao.archive.appManager.mapper.TenantYqFolderMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantYqItemMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantYqdaDocumentMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantYqdaMapper;
import cn.yuanqiao.archive.guangpanguanli.service.ArchiveService;
import cn.yuanqiao.archive.wenjianzhengli.mapper.TenantYqdaFolderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.*;

@Service
public class ArchiveServiceImpl implements ArchiveService {
    private static final Logger log = LoggerFactory.getLogger(ArchiveServiceImpl.class);
    @Autowired
    private TenantYqdaDocumentMapper documentMapper;
    @Autowired
    private TenantYqdaFolderMapper yqdaFolderMapper;
    @Autowired
    private TenantYqdaMapper yqdaMapper;
    @Autowired
    private TenantYqFolderMapper folderMapper;
    @Autowired
    private TenantYqItemMapper itemMapper;

    @Override
    @Transactional
    public void delFile(TenantCategory category, TenantYqdaDocument document) {
        log.info("删除开始ID="+document.getDocId());
        Map<String,String> map = new HashMap<>();
        map.put("IS_DEL","2");
        TenantYqdaDocument documentDel = new TenantYqdaDocument();
        documentDel.setTableName(category.getEfileTableName());
        documentDel.setDocId(document.getDocId());
        documentDel.setColNames(map);
        documentMapper.updateDocument(documentDel);
        //documentMapper.insertDocument(document);
        //documentMapper.delFileDocument(category.getEfileTableName(),document.getDocId());
        log.info("删除完成");
    }

    @Override
    @Transactional
    public void delYqda(Map<String, Object> yqda,String tenantCode,TenantCategory category) {
        String daId = yqda.get("ID").toString();
        log.info("删除开始ID="+daId);
        yqdaFolderMapper.delYqdaFolderByFolderIds(daId,tenantCode+ TenantYqdaFolder.BIGTABLENAME);
        log.info("删除关联完成");
        List<Long> id = new ArrayList<>();
        id.add(Long.valueOf(daId));
        yqdaMapper.delYqdaByIdList(tenantCode+TenantYqda.BIGTABLENAME,id);
        log.info("删除总表完成");
        yqdaMapper.delYqda(Long.valueOf(daId),category.getFileTableName());
        log.info("删除分表完成");
        TenantYqda yqda1 = new TenantYqda();
        yqda1.setTableName(category.getFileTableName()+"_BACK");
        yqda1.setColNames(convertValuesToStringAndRemoveNulls(yqda));
        yqdaMapper.insertYqdaDisperse(yqda1);
        log.info("插入备份表完成");
    }

    @Override
    @Transactional
    public void delFolder(Map<String, Object> folder, String tenantCode, TenantCategory category) {
        String daId = folder.get("ID").toString();
        log.info("删除开始ID="+daId);
        if(category.getType() == 3){
            yqdaFolderMapper.delYqdaFolderByFolderIds(daId,tenantCode+ TenantItemFolder.BIGTABLENAME);
            log.info("删除关联完成");
        }
        List<Long> id = new ArrayList<>();
        id.add(Long.valueOf(daId));
        folderMapper.delFolderByIdList(tenantCode+TenantYqFolder.BIGTABLENAME,id);
        log.info("删除总表完成");
        folderMapper.delYqFolder(Long.valueOf(daId),category.getFolderTableName());
        log.info("删除分表完成");
        TenantYqFolder yqda1 = new TenantYqFolder();
        yqda1.setTableName(category.getFolderTableName()+"_BACK");
        yqda1.setColNames(convertValuesToStringAndRemoveNulls(folder));
        folderMapper.insertYqFolderDisperse(yqda1);
        log.info("插入备份表完成");
    }

    @Override
    @Transactional
    public void delItem(Map<String, Object> item, String tenantCode, TenantCategory category) {
        String daId = item.get("ID").toString();
        log.info("删除开始ID="+daId);
        List<Long> id = new ArrayList<>();
        id.add(Long.valueOf(daId));
        itemMapper.delItemByIdList(tenantCode+TenantYqItem.BIGTABLENAME,id);
        log.info("删除总表完成");
        itemMapper.delItemByIds(daId,category.getItemTableName());
        log.info("删除分表完成");
        TenantYqItem yqda1 = new TenantYqItem();
        yqda1.setTableName(category.getItemTableName()+"_BACK");
        yqda1.setColNames(convertValuesToStringAndRemoveNulls(item));
        itemMapper.insertItemDisperse(yqda1);
        log.info("插入备份表完成");
    }

    private Map<String, String> convertValuesToStringAndRemoveNulls(Map<String, Object> originalMap) {
        // 创建一个新的 Map 来存储结果
        Map<String, String> resultMap = new HashMap<>();

        // 遍历原始 Map
        for (Map.Entry<String, Object> entry : originalMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            // 检查值是否为 null
            if (value != null) {
                // 将非 null 值转换为字符串并存入新 Map
                resultMap.put(key, String.valueOf(value));
            }
        }

        // 返回处理后的新 Map
        return resultMap;
    }
}
